package com.carlinfo.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.users.dao.ICourseDAO;
import com.carlinfo.users.pojo.ACourse;
import com.carlinfo.users.service.ICourseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;

/**
 * @author l1103
 *	首页课程service实现类
 */
@Service("courseService")
public class CourseServiceImpl implements ICourseService {

	@Resource(name = "courseDAO")
	private ICourseDAO courseDAO;
	
	/**
	 * 保存一条数据
	 * */
	@Override
	public JSONObject saveOneService(ACourse course) {
		int res = courseDAO.saveOne(course);
		JSONObject resultJSON = new JSONObject();
		if(res>0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("deffect", res);
			dataJSON.put("id", course.getId());
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", 0);
			resultJSON.put("info", "成功");
			
		}else {
			resultJSON.put("code", -1);
			resultJSON.put("info", "失败");
		}
		
		return resultJSON;
	}

	/**
	 * 更新一条数据
	 * */
	@Override
	public JSONObject updateOneService(ACourse course) {
		JSONObject resultJSON = new JSONObject();
		int res = this.courseDAO.updateOne(course);
		
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("dffect", res + "");
			dataJSON.put("id", course.getId() + "");
			/*把所需数据存放到resultJSON中*/
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			
			resultJSON.put("code", "-1");
			resultJSON.put("info", "失败"	);
		}
		return resultJSON;
	}

	 /**
	  * 删除一条数据
	  * */
	@Override
	public JSONObject deleteOneService(Map<String, Object> condMap) {
		JSONObject resultJSON = new JSONObject();
		int res = this.courseDAO.deleteOne(condMap);
		
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("dffect", res + "");
			dataJSON.put("id", condMap.get("id") + "");
			/*把所需数据存放到resultJSON中*/
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			
			resultJSON.put("code", "-1");
			resultJSON.put("info", "失败"	);
		}
		return resultJSON;
	}

	/**
	 * 查询一条数据
	 * */
	@Override
	public ACourse findOneProdService(Map<String, Object> conMap) {
		return courseDAO.findOne(conMap);
	}

	/**
	 * 查询多条数据信息
	 * 分页
	 * */
	@Override
	public List<ACourse> findCondProdList(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			Page<ACourse> page = PageMethod.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());//当前页  每页多少条
			List<ACourse> courseList = this.courseDAO.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return courseList;
		}
		return this.courseDAO.findCondList(condMap);
	}

}
